package com.yeyks.app.controller.base.activity;

import cn.hutool.core.collection.CollectionUtil;
import com.yeyks.activity.dal.domain.ActivityList;
import com.yeyks.activity.dal.domain.ActivityNodeUserRecord;
import com.yeyks.activity.service.ActivityNodeUserRecordService;
import com.yeyks.app.annotation.LoadUserId;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.constants.activity.RedisKeyPreConstant;
import com.yeyks.common.param.consumer.UserIdParam;
import com.yeyks.common.utils.TryCatchUtil;
import com.yeyks.commonReference.append.statistic.StatisticBaseService;
import com.yeyks.commonReference.pojo.param.activity.HotelJipInvite.ActivityHotelJipInviteActiveCardParam;
import com.yeyks.commonReference.pojo.param.activity.HotelJipInvite.ActivityHotelJipInviteStaticsticParam;
import com.yeyks.commonReference.pojo.vo.activity.HotelJipInvite.GetUserActivityInfoVo;
import com.yeyks.commonReference.service.activity.ActivityBaseService;
import com.yeyks.commonReference.service.consumer.UserMemberCardService;
import com.yeyks.consumer.service.UserInviteRelationService;
import com.yeyks.hotel.dal.domain.HotelVipShow;
import com.yeyks.hotel.service.HotelVipShowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "jip邀请活动v4.2.0", description = "jip邀请活动")
@RestController
@RequestMapping(value = "/app/activity/hotel/jip/invite")
public class ActivityHotelJipInviteController {

    @Autowired
    private HotelVipShowService hotelVipShowService;
    @Autowired
    private UserInviteRelationService userInviteRelationService;
    @Autowired
    private StatisticBaseService statisticBaseService;
    @Autowired
    private UserMemberCardService userMemberCardService;
    @Autowired
    private ActivityNodeUserRecordService activityNodeUserRecordService;
    @Autowired
    private ActivityBaseService activityBaseService;

    private void checkEnd() {
        activityBaseService.checkActivityEndDate("activity_hotel_jip_invite");
    }

    @ApiOperation(value = "活动详情v4.2.0", notes = "活动详情")
    @PostMapping("/detail")
    @ResponseBody
    public AjaxResponseBody<ActivityList> getActivityDetail() {
//        checkEnd();
        return AjaxResponseBody.success(activityBaseService.getActivityDetail("activity_hotel_jip_invite"));
    }

    @ApiOperation(value = "统计v4.2.0", notes = "统计")
    @PostMapping("/staticstic")
    public AjaxResponseBody<Boolean> statistic(ActivityHotelJipInviteStaticsticParam param) {
//        checkEnd();
        statisticBaseService.statisticIncrement(
                RedisKeyPreConstant.STATISTICAL_DATA_MIDDLE_ACTIVITY,
                RedisKeyPreConstant.ACTIVITY_HOTEL_JIP_INVITE_COUNT,
                param.getType()
        );
        return AjaxResponseBody.success(true);
    }

    @ApiOperation(value = "统计转发v4.2.0", notes = "统计转发")
    @PostMapping("/staticstic/for/forward")
    public AjaxResponseBody<Boolean> statisticForForward(@LoadUserId UserIdParam param) {
        statistic(new ActivityHotelJipInviteStaticsticParam().setType("3"));
        //统计转发人数
        TryCatchUtil.tryCatch(() -> {
            activityNodeUserRecordService.save(
                    new ActivityNodeUserRecord()
                            .setActivityCode("activity_hotel_jip_invite")
                            .setNode("invite_forward")
                            .setUserId(param.getUserId())
            );
        });
        return AjaxResponseBody.success(true);
    }

    @ApiOperation(value = "活动推荐酒店v4.2.0", notes = "活动推荐酒店")
    @PostMapping("/show/hotel/list")
    public AjaxResponseBody<List<HotelVipShow>> getShowHotelList() {
        return AjaxResponseBody.success(hotelVipShowService.getShowHotelList());
    }

    @ApiOperation(value = "获取当前用户的邀请人数v4.2.0", notes = "获取当前用户的邀请人数")
    @PostMapping("/user/activity/info")
    public AjaxResponseBody<GetUserActivityInfoVo> getUserActivityInfo(@LoadUserId UserIdParam param) {
        GetUserActivityInfoVo vo = new GetUserActivityInfoVo();
        vo.setCount(userInviteRelationService.getInviteUserCount(CollectionUtil.newArrayList(5), param.getUserId()));
        return AjaxResponseBody.success(vo);
    }

    @ApiOperation(value = "激活获得的jip卡v4.2.0", notes = "激活获得的jip卡")
    @PostMapping("/active/card")
    public AjaxResponseBody<Boolean> activeCard(@LoadUserId @Validated ActivityHotelJipInviteActiveCardParam param) {
        checkEnd();
        userMemberCardService.inviteActivityUserBindCard(param);
        return AjaxResponseBody.success(true);
    }

}
